package com.software.service.impl;

import java.util.List;

import org.codehaus.plexus.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.software.dao.AgentDao;
import com.software.dao.StuMngDao;
import com.software.model.DrmAgentAmountLog;
import com.software.model.DrmArticles;
import com.software.model.DrmUser;
import com.software.model.PageListModel;
import com.software.model.PageModel;
import com.software.service.AgentService;
import com.software.utils.Constants;

@Transactional
@Service("agentService")
public class AgentServiceImpl implements AgentService{
	
	@Autowired 
	private AgentDao agentDao; 
	
	@Autowired 
	private StuMngDao stuMngDao; 
	
	private int beginRow;
	
	private int endRow;
	
   @Override
   public PageListModel queryInfo(PageModel vo){
	   // 统计总数
	   Long cnt = agentDao.queryArticlesCnt(vo);
	   
	   PageListModel val = new PageListModel();
	   
	   if (0 < cnt) {
			vo.setTotalNum(Integer.valueOf(cnt.toString()));

			beginRow = (vo.getPageNo() - 1) * vo.getPageSize();
			val.setPage(vo);

			endRow = vo.getPageNo() * (vo.getPageSize());

			System.out.println("beginRow = " + beginRow);

			System.out.println("endRow = " + endRow);
			vo.setStartLine(beginRow);
			vo.setTotalLine(vo.getPageSize());
			List<DrmArticles> list = agentDao.queryInfoList(vo);
			
			for (DrmArticles img : list) {
				val.getShowData().add(img);
			}

		}
	   
//	   List<DrmArticles> list = agentDao.queryInfoList(vo);
//	   
//	   PageListModel val = new PageListModel();
//	   vo.setTotalNum(list.size());
//	   if (null != list) {
//		   
//		   
//		   if(0 == (list.size() % vo.getPageSize())) {
//			   beginRow = (vo.getPageNo() - 1) *  vo.getPageSize() + 1;
////			   vo.setPageNo(list.size() / vo.getPageSize());
//		   } else {
//			   beginRow = (vo.getPageNo() - 1) * vo.getPageSize() + 1;
////			   vo.setPageNo(list.size() / vo.getPageSize() + 1);
//		   }
//		   val.setPage(vo);
//		   
//		   endRow = vo.getPageNo() * (vo.getPageSize());
//		   
//		   System.out.println("beginRow = " + beginRow);
//		   
//		   System.out.println("endRow = " + endRow);
//		   
//		   for (int i = 0; i < list.size();i++ ) {
//			   if (beginRow <= (i+1) && i < endRow) {
//				   val.getShowData().add(list.get(i));
//			   }
//		   }
//	   }
	   return val;
   }
   
   @Override
   public void updateInfoDel(List<DrmArticles> list) {
	   for (DrmArticles data : list ) {
		   
		   agentDao.updateDrmArticlesDel(data);
	   }
   }
   
   @Override
   public void updateArtHeader(List<DrmArticles> list) {
	   for (DrmArticles data : list ) {
		   
		   agentDao.updateDrmArticlesheader(data);
	   }
   }
   
   @Override
   public void saveDrmArticles(DrmArticles data){
	   agentDao.insertDrmArticles(data);
   }
   
   @Override
   public void updateDrmArticles(DrmArticles data){
	   
		agentDao.updateDrmArticles(data);
	}
   
   @Override
   public void updateArtCnt(DrmArticles data){
	   agentDao.updateDrmArticlesCnt(data);
   }
   
   @Override
   public List<DrmArticles> searchInfoHomePage(PageModel vo){
	   if(Constants.ARTICLE_TP_1.equals(vo.getArticleTp())) {
		   vo.setStartLine(0);
		   vo.setTotalLine(7);
	   } else {
		   vo.setStartLine(0);
		   vo.setTotalLine(2);
	   }

	   List<DrmArticles> list = agentDao.queryInfoList(vo);
	 
	   return list;
   }
   
   @Override
   public DrmArticles searchInfoDetails(String id){
	   DrmArticles param = new DrmArticles();
	   if(StringUtils.isNotBlank(id)) {
		   param.setId(new Long(id));
	   }
	  
	   param = agentDao.queryInfoDetails(param);
	 
	   return param;
   }
   
   @Override
   public DrmUser searchUserAmount(Long userId){
	   
	   return agentDao.queryAmountData(userId);
   }
   
   @Override
   public List<DrmAgentAmountLog> searchUserAmountLog(Long userId){
	   return stuMngDao.qureyAmountLogList(userId);
   }
   
}